﻿using System;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Sunny.UI;
using LogisticsAndong.Comm;
using LogisticsAndong.Business;
using LogisticsAndong.Model;
using Leeder.Logger;
using LeederGlobal;

namespace LogisticsAndong.Forms
{
    public partial class FrmSystemSet : UIPage
    {
        /// <summary>
        /// 判断窗体是否首次加载
        /// </summary>
        bool isLoad = true;

        /// <summary>
        /// 日志
        /// </summary>
        public LogOperate LogObj => Log.GetLogger(this.GetType());

        MaterialDirectionBLL bllMaterialDirection = new MaterialDirectionBLL();
        SignalLampDirectionBLL bllSignalLampDirection = new SignalLampDirectionBLL();
        UnpackingDirectionBLL bllUnpackingDirection = new UnpackingDirectionBLL();

        UnpackingDirectionModel entityUnpackingDirection = new UnpackingDirectionModel();

        AutoSizeFormClass asc = new AutoSizeFormClass();

        public FrmSystemSet()
        {
            InitializeComponent();
        }

        private void FrmSystemSet_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;

            //asc.controllInitializeSize(this.tabPage9);

            //开捆
            dgvKk.AutoGenerateColumns = false;
            dgvKk.AllowUserToAddRows = false;
            dgvKk.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvKk.ReadOnly = true;
            dgvKk.AddColumn("序号", "ID", 33);
            dgvKk.AddColumn("工程名称", "UnpackingName", 33);
            dgvKk.AddColumn("IP地址", "IpAddress", 34);
            //dgvKk.AddColumn("位置", "exception_description", 25);

            //顺引顺建
            dgvDmsy.AutoGenerateColumns = false;
            dgvDmsy.AllowUserToAddRows = false;
            dgvDmsy.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvDmsy.ReadOnly = true;
            dgvDmsy.AddColumn("序号", "ID", 5);
            dgvDmsy.AddColumn("工程名称", "MaterialName", 20);
            dgvDmsy.AddColumn("IP地址", "IpAddress", 15);
            dgvDmsy.AddColumn("出发台数", "TriggerCount", 15);
            dgvDmsy.AddColumn("每次配送数", "OnceTransportCount", 15);
            dgvDmsy.AddColumn("车型标识", "CarMark", 15);
            dgvDmsy.AddColumn("信号源标识", "SignalMark", 15);

            dgvXmsy.AutoGenerateColumns = false;
            dgvXmsy.AllowUserToAddRows = false;
            dgvXmsy.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvXmsy.ReadOnly = true;
            dgvXmsy.AddColumn("序号", "ID", 5);
            dgvXmsy.AddColumn("工程名称", "MaterialName", 20);
            dgvXmsy.AddColumn("IP地址", "IpAddress", 15);
            dgvXmsy.AddColumn("出发台数", "TriggerCount", 15);
            dgvXmsy.AddColumn("每次配送数", "OnceTransportCount", 15);
            dgvXmsy.AddColumn("车型标识", "CarMark", 15);
            dgvXmsy.AddColumn("信号源标识", "SignalMark", 15);

            dgvZwsy.AutoGenerateColumns = false;
            dgvZwsy.AllowUserToAddRows = false;
            dgvZwsy.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvZwsy.ReadOnly = true;
            dgvZwsy.AddColumn("序号", "ID", 5);
            dgvZwsy.AddColumn("工程名称", "MaterialName", 20);
            dgvZwsy.AddColumn("IP地址", "IpAddress", 15);
            dgvZwsy.AddColumn("出发台数", "TriggerCount", 15);
            dgvZwsy.AddColumn("每次配送数", "OnceTransportCount", 15);
            dgvZwsy.AddColumn("车型标识", "CarMark", 15);
            dgvZwsy.AddColumn("信号源标识", "SignalMark", 15);

            dgvNzp.AutoGenerateColumns = false;
            dgvNzp.AllowUserToAddRows = false;
            dgvNzp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvNzp.ReadOnly = true;
            dgvNzp.AddColumn("序号", "ID", 5);
            dgvNzp.AddColumn("工程名称", "MaterialName", 20);
            dgvNzp.AddColumn("IP地址", "IpAddress", 15);
            dgvNzp.AddColumn("出发台数", "TriggerCount", 10);
            dgvNzp.AddColumn("每次配送数", "OnceTransportCount", 10);
            dgvNzp.AddColumn("工程数", "ProjectCount", 10);
            dgvNzp.AddColumn("车型标识", "CarMark", 15);
            dgvNzp.AddColumn("信号源标识", "SignalMark", 15);

            dgvPx.AutoGenerateColumns = false;
            dgvPx.AllowUserToAddRows = false;
            dgvPx.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvPx.ReadOnly = true;
            dgvPx.AddColumn("序号", "ID", 5);
            dgvPx.AddColumn("工程名称", "MaterialName", 20);
            dgvPx.AddColumn("IP地址", "IpAddress", 15);
            dgvPx.AddColumn("出发台数", "TriggerCount", 15);
            dgvPx.AddColumn("每次配送数", "OnceTransportCount", 15);
            dgvPx.AddColumn("车型标识", "CarMark", 15);
            dgvPx.AddColumn("信号源标识", "SignalMark", 15);

            //拉绳安灯
            dgvRdcf.AutoGenerateColumns = false;
            dgvRdcf.AllowUserToAddRows = false;
            dgvRdcf.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvRdcf.ReadOnly = true;
            dgvRdcf.AddColumn("序号", "ID", 5);
            dgvRdcf.AddColumn("工程名称", "LampName", 15);
            dgvRdcf.AddColumn("拉绳输入地址", "ControlInputAddress", 10);
            dgvRdcf.AddColumn("绿灯输出地址", "GreenOutputAddress", 10);
            dgvRdcf.AddColumn("红灯输出地址", "RedOutputAddress", 10);
            dgvRdcf.AddColumn("出发台数", "TriggerCount", 10);
            dgvRdcf.AddColumn("出发延时台数", "TriggerDelayCount", 10);
            dgvRdcf.AddColumn("结束延时台数", "EndDelayCount", 10);
            dgvRdcf.AddColumn("单数/双数链", "ChainMark", 9);
            dgvRdcf.AddColumn("状态", "Status", 9);
            //dgvRdcf.AddColumn("链数标识", "ChainMark", 1);
            //dgvRdcf.AddColumn("状态标识", "Status", 1);

            dgvTjshzgcf.AutoGenerateColumns = false;
            dgvTjshzgcf.AllowUserToAddRows = false;
            dgvTjshzgcf.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvTjshzgcf.ReadOnly = true;
            dgvTjshzgcf.AddColumn("序号", "ID", 5);
            dgvTjshzgcf.AddColumn("工程名称", "LampName", 15);
            dgvTjshzgcf.AddColumn("拉绳输入地址", "ControlInputAddress", 10);
            dgvTjshzgcf.AddColumn("绿灯输出地址", "GreenOutputAddress", 10);
            dgvTjshzgcf.AddColumn("红灯输出地址", "RedOutputAddress", 10);
            dgvTjshzgcf.AddColumn("出发台数", "TriggerCount", 10);
            dgvTjshzgcf.AddColumn("出发延时台数", "TriggerDelayCount", 10);
            dgvTjshzgcf.AddColumn("结束延时台数", "EndDelayCount", 10);
            dgvTjshzgcf.AddColumn("单数/双数链", "ChainMark", 9);
            dgvTjshzgcf.AddColumn("状态", "Status", 9);
            //dgvTjshzgcf.AddColumn("链数标识", "ChainMark", 1);
            //dgvTjshzgcf.AddColumn("状态标识", "Status", 1);

            dgvPcjhcf.AutoGenerateColumns = false;
            dgvPcjhcf.AllowUserToAddRows = false;
            dgvPcjhcf.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvPcjhcf.ReadOnly = true;
            dgvPcjhcf.AddColumn("序号", "ID", 5);
            dgvPcjhcf.AddColumn("工程名称", "LampName", 15);
            dgvPcjhcf.AddColumn("拉绳输入地址", "ControlInputAddress", 10);
            dgvPcjhcf.AddColumn("绿灯输出地址", "GreenOutputAddress", 10);
            dgvPcjhcf.AddColumn("红灯输出地址", "RedOutputAddress", 10);
            dgvPcjhcf.AddColumn("出发台数", "TriggerCount", 10);
            dgvPcjhcf.AddColumn("出发延时台数", "TriggerDelayCount", 10);
            dgvPcjhcf.AddColumn("结束延时台数", "EndDelayCount", 10);
            dgvPcjhcf.AddColumn("单数/双数链", "ChainMark", 9);
            dgvPcjhcf.AddColumn("状态", "Status", 9);
            //dgvPcjhcf.AddColumn("链数标识", "ChainMark", 1);
            //dgvPcjhcf.AddColumn("状态标识", "Status", 1);

            dgvFxdwcf.AutoGenerateColumns = false;
            dgvFxdwcf.AllowUserToAddRows = false;
            dgvFxdwcf.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvFxdwcf.ReadOnly = true;
            dgvFxdwcf.AddColumn("序号", "ID", 5);
            dgvFxdwcf.AddColumn("工程名称", "LampName", 15);
            dgvFxdwcf.AddColumn("拉绳输入地址", "ControlInputAddress", 10);
            dgvFxdwcf.AddColumn("绿灯输出地址", "GreenOutputAddress", 10);
            dgvFxdwcf.AddColumn("红灯输出地址", "RedOutputAddress", 10);
            dgvFxdwcf.AddColumn("出发台数", "TriggerCount", 10);
            dgvFxdwcf.AddColumn("出发延时台数", "TriggerDelayCount", 10);
            dgvFxdwcf.AddColumn("结束延时台数", "EndDelayCount", 10);
            dgvFxdwcf.AddColumn("单数/双数链", "ChainMark", 9);
            dgvFxdwcf.AddColumn("状态", "Status", 9);
            //dgvFxdwcf.AddColumn("链数标识", "ChainMark", 1);
            //dgvFxdwcf.AddColumn("状态标识", "Status", 1);

            dgvCxdwcf.AutoGenerateColumns = false;
            dgvCxdwcf.AllowUserToAddRows = false;
            dgvCxdwcf.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvCxdwcf.ReadOnly = true;
            dgvCxdwcf.AddColumn("序号", "ID", 5);
            dgvCxdwcf.AddColumn("工程名称", "LampName", 15);
            dgvCxdwcf.AddColumn("拉绳输入地址", "ControlInputAddress", 10);
            dgvCxdwcf.AddColumn("绿灯输出地址", "GreenOutputAddress", 10);
            dgvCxdwcf.AddColumn("红灯输出地址", "RedOutputAddress", 10);
            dgvCxdwcf.AddColumn("出发台数", "TriggerCount", 10);
            dgvCxdwcf.AddColumn("出发延时台数", "TriggerDelayCount", 10);
            dgvCxdwcf.AddColumn("结束延时台数", "EndDelayCount", 10);
            dgvCxdwcf.AddColumn("单数/双数链", "ChainMark", 9);
            dgvCxdwcf.AddColumn("状态", "Status", 9);
            //dgvCxdwcf.AddColumn("链数标识", "ChainMark", 1);
            //dgvCxdwcf.AddColumn("状态标识", "Status", 1);

            dgvXwcf.AutoGenerateColumns = false;
            dgvXwcf.AllowUserToAddRows = false;
            dgvXwcf.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvXwcf.ReadOnly = true;
            dgvXwcf.AddColumn("序号", "ID", 5);
            dgvXwcf.AddColumn("工程名称", "LampName", 15);
            dgvXwcf.AddColumn("拉绳输入地址", "ControlInputAddress", 10);
            dgvXwcf.AddColumn("绿灯输出地址", "GreenOutputAddress", 10);
            dgvXwcf.AddColumn("红灯输出地址", "RedOutputAddress", 10);
            dgvXwcf.AddColumn("出发台数", "TriggerCount", 10);
            dgvXwcf.AddColumn("出发延时台数", "TriggerDelayCount", 10);
            dgvXwcf.AddColumn("结束延时台数", "EndDelayCount", 10);
            dgvXwcf.AddColumn("单数/双数链", "ChainMark", 9);
            dgvXwcf.AddColumn("状态", "Status", 9);
            //dgvXwcf.AddColumn("链数标识", "ChainMark", 1);
            //dgvXwcf.AddColumn("状态标识", "Status", 1);

            dgvKk.DataSource = GolbalVariable.UnpackingDirectionBag.Where(x => x.DataType == "链数").OrderBy(x => x.ID).ToList();

            dgvDmsy.DataSource = GolbalVariable.MaterialDirectionBag.Where(x => x.TypeId == 1).OrderBy(x => x.ID).ToList();
            dgvXmsy.DataSource = GolbalVariable.MaterialDirectionBag.Where(x => x.TypeId == 2).OrderBy(x => x.ID).ToList();
            dgvZwsy.DataSource = GolbalVariable.MaterialDirectionBag.Where(x => x.TypeId == 3).OrderBy(x => x.ID).ToList();
            dgvNzp.DataSource = GolbalVariable.MaterialDirectionBag.Where(x => x.TypeId == 4).OrderBy(x => x.ID).ToList();
            dgvPx.DataSource = GolbalVariable.MaterialDirectionBag.Where(x => x.TypeId == 5).OrderBy(x => x.ID).ToList();

            dgvRdcf.DataSource = GolbalVariable.SignalLampDirectionBag.Where(x => x.TypeId == 1).OrderBy(x => x.ID).ToList();
            dgvTjshzgcf.DataSource = GolbalVariable.SignalLampDirectionBag.Where(x => x.TypeId == 2).OrderBy(x => x.ID).ToList();
            dgvPcjhcf.DataSource = GolbalVariable.SignalLampDirectionBag.Where(x => x.TypeId == 3).OrderBy(x => x.ID).ToList();
            dgvFxdwcf.DataSource = GolbalVariable.SignalLampDirectionBag.Where(x => x.TypeId == 4).OrderBy(x => x.ID).ToList();
            dgvCxdwcf.DataSource = GolbalVariable.SignalLampDirectionBag.Where(x => x.TypeId == 5).OrderBy(x => x.ID).ToList();
            dgvXwcf.DataSource = GolbalVariable.SignalLampDirectionBag.Where(x => x.TypeId == 6).OrderBy(x => x.ID).ToList();

            UpdateCells(dgvRdcf);
            UpdateCells(dgvTjshzgcf);
            UpdateCells(dgvPcjhcf);
            UpdateCells(dgvFxdwcf);
            UpdateCells(dgvCxdwcf);
            UpdateCells(dgvXwcf);
        }

        public void UpdateCells(UIDataGridView dgv)
        {
            /************
            if (dgv.Rows.Count > 0)
                dgv.Rows[0].Selected = false;

            foreach (DataGridViewRow item in dgv.Rows)
            {
                SignalLampDirectionModel model = item.DataBoundItem as SignalLampDirectionModel;
                if (model.ChainMark == "0")
                    item.Cells["单数/双数链"].Value = "全部";
                else if (model.ChainMark == "1")
                    item.Cells["单数/双数链"].Value = "单链";
                else if (model.ChainMark == "2")
                    item.Cells["单数/双数链"].Value = "双链";
                if (model.Status == 0)
                    item.Cells["状态"].Value = "停用";
                else
                    item.Cells["状态"].Value = "启用";
            }
            //dgv.Update();
            ******************/
        }

        /// <summary>
        /// 开捆参数设置
        /// </summary>
        /// <param name="entityMaterialDirection">开捆对象</param>
        private void unpackingEdit(UnpackingDirectionModel entityUnpackingDirection = null)
        {
            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;

            if (entityUnpackingDirection != null)
            {
                option.Text = "参数设置(" + entityUnpackingDirection.UnpackingName + ")";
                option.AddText("UnpackingName", "工程名称", entityUnpackingDirection.UnpackingName, true);
                option.AddText("IpAddress", "IP地址", entityUnpackingDirection.IpAddress, false);
                //option.AddText("TriggerCount", "位置", null, false);
            }else
            {
                option.Text = "新增开捆";
                option.AddText("UnpackingName", "工程名称", null, true);
                option.AddText("IpAddress", "IP地址", null, false);
                //option.AddText("TriggerCount", "位置", null, false);
            }


            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                if(entityUnpackingDirection != null)
                {
                    entityUnpackingDirection.UnpackingName = frm["UnpackingName"].ToString();
                    entityUnpackingDirection.IpAddress = frm["IpAddress"].ToString();
                    if (bllUnpackingDirection.Update(entityUnpackingDirection).IsSuccess)
                        UIMessageTip.ShowOk("设置成功");
                    else
                        UIMessageBox.ShowError("设置失败！");
                    LogObj.Info("开捆参数设置(" + entityUnpackingDirection.UnpackingName + ")");
                }else
                {
                    entityUnpackingDirection = new UnpackingDirectionModel();
                    entityUnpackingDirection.UnpackingName = frm["UnpackingName"].ToString();
                    entityUnpackingDirection.IpAddress = frm["IpAddress"].ToString();
                    bllUnpackingDirection.Add(entityUnpackingDirection);
                    if (bllUnpackingDirection.Add(entityUnpackingDirection).IsSuccess)
                        UIMessageTip.ShowOk("新增成功");
                    else
                        UIMessageBox.ShowError("新增失败！");
                    LogObj.Info("新增开捆工程(" + entityUnpackingDirection.UnpackingName + ")");
                }
            }
            frm.Dispose();
        }

        /// <summary>
        /// 顺引顺建/内制品/排序工程参数设置
        /// </summary>
        /// <param name="entityMaterialDirection">部品对象</param>
        private void materialDirectionEdit(MaterialDirectionModel entityMaterialDirection = null, int typeId = 0)
        {
            var var = GolbalVariable.OpcTagBag.Where(x => x.GroupId == 1).ToArray();
            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;
            if (entityMaterialDirection != null)
            {
                option.Text = "参数设置(" + entityMaterialDirection.MaterialName + ")";
                option.AddText("MaterialName", "工程名称", entityMaterialDirection.MaterialName, true);
                option.AddText("IpAddress", "IP地址", entityMaterialDirection.IpAddress, false);
                option.AddInteger("TriggerCount", "出发台数", entityMaterialDirection.TriggerCount, true);
                option.AddInteger("OnceTransportCount", "每次配送数", entityMaterialDirection.OnceTransportCount, true);
                if (typeId == 4)
                    option.AddInteger("ProjectCount", "工程数", entityMaterialDirection.ProjectCount, true); //内制品多一个工程数
                option.AddText("CarMark", "车型标识", entityMaterialDirection.CarMark, false);
                option.AddCombobox("SignalMark", "信号源标识", var, "SignalMark", "SignalMark", entityMaterialDirection.SignalMark);
            }
            else
            {
                option.Text = "新增工程";
                option.AddText("MaterialName", "工程名称", null, true);
                option.AddText("IpAddress", "IP地址", null, false);
                option.AddInteger("TriggerCount", "出发台数", 0, true);
                option.AddInteger("OnceTransportCount", "每次配送数", 0, true);
                if (typeId == 4)
                    option.AddInteger("ProjectCount", "工程数", 0, true); //内制品多一个工程数
                option.AddText("CarMark", "车型标识", null, false);
                option.AddCombobox("SignalMark", "信号源标识", var, "SignalMark", "SignalMark", null);
            }
            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                if (entityMaterialDirection != null)
                {
                    entityMaterialDirection.IpAddress = frm["IpAddress"].ToString();
                    entityMaterialDirection.TriggerCount = int.Parse(frm["TriggerCount"].ToString());
                    entityMaterialDirection.OnceTransportCount = int.Parse(frm["OnceTransportCount"].ToString());
                    entityMaterialDirection.CarMark = frm["CarMark"].ToString();
                    entityMaterialDirection.SignalMark = frm["SignalMark"].ToString();
                    if (typeId == 4)
                        entityMaterialDirection.ProjectCount = int.Parse(frm["ProjectCount"].ToString()); //内制品多一个工程数

                    if (bllMaterialDirection.Update(entityMaterialDirection).IsSuccess)
                        UIMessageTip.ShowOk("设置成功");
                    else
                        UIMessageBox.ShowError("设置失败！");
                    LogObj.Info("顺引顺建/内制品/排序工程参数设置(" + entityMaterialDirection.MaterialName + ")");
                }
                else
                {
                    entityMaterialDirection = new MaterialDirectionModel();
                    entityMaterialDirection.IpAddress = frm["IpAddress"].ToString();
                    entityMaterialDirection.TriggerCount = int.Parse(frm["TriggerCount"].ToString());
                    entityMaterialDirection.OnceTransportCount = int.Parse(frm["OnceTransportCount"].ToString());
                    entityMaterialDirection.CarMark = frm["CarMark"].ToString();
                    entityMaterialDirection.SignalMark = frm["SignalMark"].ToString();
                    if (typeId == 4)
                        entityMaterialDirection.ProjectCount = int.Parse(frm["ProjectCount"].ToString()); //内制品多一个工程数
                    switch (typeId)
                    {
                        case 1:
                            entityMaterialDirection.TypeId = 1;
                            entityMaterialDirection.TypeName = "东面顺引工程";
                            break;
                        case 2:
                            entityMaterialDirection.TypeId = 2;
                            entityMaterialDirection.TypeName = "西面顺引工程";
                            break;
                        case 3:
                            entityMaterialDirection.TypeId = 3;
                            entityMaterialDirection.TypeName = "中物顺建工程";
                            break;
                        case 4:
                            entityMaterialDirection.TypeId = 4;
                            entityMaterialDirection.TypeName = "内制品工程";
                            break;
                        case 5:
                            entityMaterialDirection.TypeId = 5;
                            entityMaterialDirection.TypeName = "排序工程";
                            break;

                    }

                    if (bllMaterialDirection.Add(entityMaterialDirection).IsSuccess)
                        UIMessageTip.ShowOk("新增成功");
                    else
                        UIMessageBox.ShowError("新增失败！");
                    LogObj.Info("新增顺引顺建/内制品/排序工程(" + entityMaterialDirection.MaterialName + ")");
                }
            }
            frm.Dispose();
        }

        /// <summary>
        /// 信号灯参数设置
        /// </summary>
        /// <param name="entityMaterialDirection">信号灯对象</param>
        private void signalLampEdit(SignalLampDirectionModel entitySignalLampDirection = null,int typeId = 0)
        {
            //var var = GolbalVariable.OpcTagBag.Where(x => x.GroupId == 300001).ToArray();

            var unpackingDirections = GolbalVariable.UnpackingDirectionBag.Where(x => x.DataType == "台数").ToList();

            string[] chainMarkTypeNames = new[] { "全部", "单链", "双链"};
            string[] statuNames = new[] { "停用" ,"启用"};
            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;
            if(entitySignalLampDirection != null)
            {
                option.Text = "参数设置(" + entitySignalLampDirection.LampName + ")";
                option.AddText("LampName", "工程名称", entitySignalLampDirection.LampName, true);
                option.AddCombobox("UnpackingName", "开捆类型", unpackingDirections, "UnpackingName", "ID", entitySignalLampDirection.UnpackingId);
                option.AddText("ControlInputAddress", "拉绳输入地址", entitySignalLampDirection.ControlInputAddress, false);
                option.AddText("GreenOutputAddress", "绿灯输出地址", entitySignalLampDirection.GreenOutputAddress, false);
                option.AddText("RedOutputAddress", "红灯输出地址", entitySignalLampDirection.RedOutputAddress, false);
                option.AddInteger("TriggerCount", "出发台数", entitySignalLampDirection.TriggerCount, true);
                option.AddInteger("TriggerDelayCount", "出发延时台数", entitySignalLampDirection.TriggerDelayCount, true);
                option.AddInteger("EndDelayCount", "结束延时台数", entitySignalLampDirection.EndDelayCount, true);
                option.AddCombobox("ChainMark", "单数/双数链", chainMarkTypeNames, int.Parse(entitySignalLampDirection.ChainMark), true, true);
                option.AddCombobox("Status", "状态", statuNames, entitySignalLampDirection.Status, true, true);
            }
            else
            {
                option.Text = "新增拉绳灯";
                option.AddText("LampName", "工程名称", null, true);
                option.AddCombobox("UnpackingName", "开捆类型", unpackingDirections, "UnpackingName", "ID", unpackingDirections[0].ID);
                option.AddText("ControlInputAddress", "拉绳输入地址", null, false);
                option.AddText("GreenOutputAddress", "绿灯输出地址", null, false);
                option.AddText("RedOutputAddress", "红灯输出地址", null, false);
                option.AddInteger("TriggerCount", "出发台数", 0, true);
                option.AddInteger("TriggerDelayCount", "出发延时台数", 0, true);
                option.AddInteger("EndDelayCount", "结束延时台数", 0, true);
                option.AddCombobox("ChainMark", "单数/双数链", chainMarkTypeNames, 0, true, true);
                option.AddCombobox("Status", "状态", statuNames, 1, true, true);
            }

            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                var unpackingDirection = GolbalVariable.UnpackingDirectionBag.Where(x => x.ID == int.Parse(frm["UnpackingName"].ToString())).FirstOrDefault();
                if (entitySignalLampDirection != null)
                {
                    entitySignalLampDirection.LampName = frm["LampName"].ToString();
                    entitySignalLampDirection.ControlInputAddress = frm["ControlInputAddress"].ToString();
                    entitySignalLampDirection.GreenOutputAddress = frm["GreenOutputAddress"].ToString();
                    entitySignalLampDirection.RedOutputAddress = frm["RedOutputAddress"].ToString();
                    entitySignalLampDirection.TriggerCount = int.Parse(frm["TriggerCount"].ToString());
                    entitySignalLampDirection.TriggerDelayCount = int.Parse(frm["TriggerDelayCount"].ToString());
                    entitySignalLampDirection.EndDelayCount = int.Parse(frm["EndDelayCount"].ToString());
                    entitySignalLampDirection.ChainMark = frm["ChainMark"].ToString();
                    entitySignalLampDirection.Status = int.Parse(frm["Status"].ToString());

                    entitySignalLampDirection.TypeId = unpackingDirection.TypeId;
                    entitySignalLampDirection.TypeName = unpackingDirection.UnpackingName;
                    entitySignalLampDirection.UnpackingId = unpackingDirection.ID;

                    if (bllSignalLampDirection.Update(entitySignalLampDirection).IsSuccess)
                        UIMessageTip.ShowOk("设置成功");
                    else
                        UIMessageBox.ShowError("设置失败！");
                    LogObj.Info("拉绳灯参数设置(" + entitySignalLampDirection.LampName + ")");
                }
                else
                {
                    entitySignalLampDirection = new SignalLampDirectionModel();
                    entitySignalLampDirection.LampName = frm["LampName"].ToString();
                    entitySignalLampDirection.ControlInputAddress = frm["ControlInputAddress"].ToString();
                    entitySignalLampDirection.GreenOutputAddress = frm["GreenOutputAddress"].ToString();
                    entitySignalLampDirection.RedOutputAddress = frm["RedOutputAddress"].ToString();
                    entitySignalLampDirection.TriggerCount = int.Parse(frm["TriggerCount"].ToString());
                    entitySignalLampDirection.TriggerDelayCount = int.Parse(frm["TriggerDelayCount"].ToString());
                    entitySignalLampDirection.EndDelayCount = int.Parse(frm["EndDelayCount"].ToString());
                    entitySignalLampDirection.UnpackingId = int.Parse(frm["ID"].ToString());
                    entitySignalLampDirection.ChainMark = frm["ChainMark"].ToString();
                    entitySignalLampDirection.Status = int.Parse(frm["Status"].ToString());

                    entitySignalLampDirection.TypeId = unpackingDirection.TypeId;
                    entitySignalLampDirection.TypeName = unpackingDirection.UnpackingName;
                    entitySignalLampDirection.UnpackingId = unpackingDirection.ID;

                    if (bllSignalLampDirection.Add(entitySignalLampDirection).IsSuccess)
                        UIMessageTip.ShowOk("新增成功");
                    else
                        UIMessageBox.ShowError("新增失败！");
                    LogObj.Info("新增拉绳灯(" + entitySignalLampDirection.LampName + ")");
                }
            }
            frm.Dispose();
        }

        /// <summary>
        /// 开捆工程删除
        /// </summary>
        /// <param name="entityUnpackingDirection"></param>
        private void unpackingDelect(UnpackingDirectionModel entityUnpackingDirection)
        {
            if (bllUnpackingDirection.Delete(entityUnpackingDirection).IsSuccess)
                UIMessageTip.ShowOk("删除成功");
            else
                UIMessageBox.ShowError("删除失败！");
        }

        /// <summary>
        /// 顺引顺建工程删除
        /// </summary>
        /// <param name="entityMaterialDirection"></param>
        private void materialDirectionDelect(MaterialDirectionModel entityMaterialDirection)
        {
            if (bllMaterialDirection.Delete(entityMaterialDirection).IsSuccess)
                UIMessageTip.ShowOk("删除成功");
            else
                UIMessageBox.ShowError("删除失败！");
        }

        /// <summary>
        /// 拉绳灯删除
        /// </summary>
        /// <param name="entitySignalLampDirection"></param>
        private void signalLampDelect(SignalLampDirectionModel entitySignalLampDirection)
        {
            if (bllSignalLampDirection.Delete(entitySignalLampDirection).IsSuccess)
                UIMessageTip.ShowOk("删除成功");
            else
                UIMessageBox.ShowError("删除失败！");
        }

        /// <summary>
        /// 顺引顺建/内制品/排序工程参数设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvDmsy_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                materialDirectionEdit(dgvDmsy.CurrentRow.DataBoundItem as MaterialDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvXmsy_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                materialDirectionEdit(dgvXmsy.CurrentRow.DataBoundItem as MaterialDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvZwsy_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                materialDirectionEdit(dgvZwsy.CurrentRow.DataBoundItem as MaterialDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvNzp_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                materialDirectionEdit(dgvNzp.CurrentRow.DataBoundItem as MaterialDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvPx_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                materialDirectionEdit(dgvPx.CurrentRow.DataBoundItem as MaterialDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        /// <summary>
        /// 拉绳灯参数设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvRdcf_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                signalLampEdit(dgvRdcf.CurrentRow.DataBoundItem as SignalLampDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvTjshzgcf_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                signalLampEdit(dgvTjshzgcf.CurrentRow.DataBoundItem as SignalLampDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvPcjhcf_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                signalLampEdit(dgvPcjhcf.CurrentRow.DataBoundItem as SignalLampDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvFxdwcf_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                signalLampEdit(dgvFxdwcf.CurrentRow.DataBoundItem as SignalLampDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvCxdwcf_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                signalLampEdit(dgvCxdwcf.CurrentRow.DataBoundItem as SignalLampDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvXwcf_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                signalLampEdit(dgvXwcf.CurrentRow.DataBoundItem as SignalLampDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        /// <summary>
        /// 开捆设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvKk_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
                unpackingEdit(dgvKk.CurrentRow.DataBoundItem as UnpackingDirectionModel);
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        #region 重置按钮方法
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox1.Image = Properties.Resources.cz3;
        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.cz2;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.cz1;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pictureBox1.Image = Properties.Resources.cz2;

            if (ApplicationCache.CurrentUser.Permission == 0)
            {
                bool result = true;
                result = UIMessageDialog.ShowMessageDialog("是否确定重置所有安灯拉绳？", UILocalize.AskTitle, true, UIStyle.Red, true, Frame?.TopMost ?? false);
                if (result)
                {
                    new OpcBusiness().ResetSignalLamp();
                }
                else
                {
            
                }
            }
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");

        }
        #endregion 

        UIDataGridView dataGridView;
         private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView = (UIDataGridView)sender;
                    //若行已是选中状态就不再进行设置
                    if (dataGridView.Rows[e.RowIndex].Selected == false)
                    {
                        dataGridView.ClearSelection();
                        dataGridView.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (dataGridView.SelectedRows.Count == 1)
                    {
                        dataGridView.CurrentCell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    //弹出操作菜单
                    uiContextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void FrmSystemSet_SizeChanged(object sender, EventArgs e)
        {
            //asc.controlAutoSize(this.tabPage9);
            pictureBox1.Size = new Size(this.Height / 2, this.Height/2);
            pictureBox1.Location = new Point((this.Width - this.Height / 2) / 2, (this.Height - this.Height / 2) / 2);
        }

        private void toolStripMenuItemUpdata_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
            {
                //不同的dataGridView显示不同的编辑窗体
                switch (dataGridView.Name)
                {
                    case "dgvKk":
                        unpackingEdit(dataGridView.CurrentRow.DataBoundItem as UnpackingDirectionModel);
                        break;
                    case "dgvDmsy":
                        materialDirectionEdit(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel,1);
                        break;
                    case "dgvXmsy":
                        materialDirectionEdit(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel, 2);
                        break;
                    case "dgvZwsy":
                        materialDirectionEdit(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel, 3);
                        break;
                    case "dgvNzp":
                        materialDirectionEdit(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel, 4);
                        break;
                    case "dgvPx":
                        materialDirectionEdit(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel, 5);
                        break;
                    case "dgvRdcf":
                        signalLampEdit(dataGridView.CurrentRow.DataBoundItem as SignalLampDirectionModel, 1);
                        break;
                    case "dgvTjshzgcf":
                        signalLampEdit(dataGridView.CurrentRow.DataBoundItem as SignalLampDirectionModel, 2);
                        break;
                    case "dgvPcjhcf":
                        signalLampEdit(dataGridView.CurrentRow.DataBoundItem as SignalLampDirectionModel, 3);
                        break;
                    case "dgvFxdwcf":
                        signalLampEdit(dataGridView.CurrentRow.DataBoundItem as SignalLampDirectionModel, 4);
                        break;
                    case "dgvCxdwcf":
                        signalLampEdit(dataGridView.CurrentRow.DataBoundItem as SignalLampDirectionModel, 5);
                        break;
                    case "dgvXwcf":
                        signalLampEdit(dataGridView.CurrentRow.DataBoundItem as SignalLampDirectionModel, 6);
                        break;

                }
            }
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
            {
                //添加方法
                //不同的dataGridView显示不同的添加窗体
                switch (dataGridView.Name)
                {
                    case "dgvKk":
                        //unpackingEdit();
                        UIMessageBox.ShowWarning("该操作目前不允许！");
                        break;
                    case "dgvDmsy":
                        materialDirectionEdit();
                        break;
                    case "dgvXmsy":
                        materialDirectionEdit();
                        break;
                    case "dgvZwsy":
                        materialDirectionEdit();
                        break;
                    case "dgvNzp":
                        materialDirectionEdit();
                        break;
                    case "dgvPx":
                        materialDirectionEdit();
                        break;

                    case "dgvRdcf":
                        signalLampEdit(null, 1);
                        break;
                    case "dgvTjshzgcf":
                        signalLampEdit(null, 2);
                        break;
                    case "dgvPcjhcf":
                        signalLampEdit(null, 3);
                        break;
                    case "dgvFxdwcf":
                        signalLampEdit(null, 4);
                        break;
                    case "dgvCxdwcf":
                        signalLampEdit(null, 5);
                        break;
                    case "dgvXwcf":
                        signalLampEdit(null, 6);
                        break;
                }
            }
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission == 0)
            {
                if(dataGridView.Name == "dgvKk")
                {
                    UIMessageBox.ShowWarning("该操作目前不允许！");
                    return;
                }
                bool result = true;
                result = UIMessageDialog.ShowMessageDialog("您确定要删除该工程吗？", UILocalize.AskTitle, true, UIStyle.Red, true, Frame?.TopMost ?? false);
                if (result)
                {
                    //删除方法
                    //不同的dataGridView显示不同的编辑窗体
                    switch (dataGridView.Name)
                    {
                        case "dgvDmsy":
                            materialDirectionDelect(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel);
                            break;
                        case "dgvXmsy":
                            materialDirectionDelect(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel);
                            break;
                        case "dgvZwsy":
                            materialDirectionDelect(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel);
                            break;
                        case "dgvNzp":
                            materialDirectionDelect(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel);
                            break;
                        case "dgvPx":
                            materialDirectionDelect(dataGridView.CurrentRow.DataBoundItem as MaterialDirectionModel);
                            break;
                        default:
                            signalLampDelect(dataGridView.CurrentRow.DataBoundItem as SignalLampDirectionModel);
                            break;

                    }
                }
                else
                {

                }
            }
            else
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
        }

        private void dgvRdcf_DataSourceChanged(object sender, EventArgs e)
        {
            UpdateCells((UIDataGridView)sender);
        }
    }
}
